
//
//  UpdateNet.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/15.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "UpdateNet.h"
#import "BasicNetManager.h"
#import "BLHeader.h"
#import <YYModel.h>
#import "Address.h"

@implementation UpdateNet


+ (void)updateUserInfo:(NSDictionary *)param completer:(void(^)(id resultObject, NSError *error)) completer {
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:UPDATEUSERINFO param:param completer:^(id result, NSError *error) {
        if(completer) {
            if (result) {
                [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"signature"] forKey:USERSIGN];
                [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"userName"] forKey:USERNAME];
                [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"userImg"] forKey:USERIMG];
                [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"userNum"] forKey:USERNUM];
                [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"userTel"] forKey:USERTEL];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [[NSNotificationCenter defaultCenter] postNotificationName:UPDATEUISERINFONOTIFICATION object:nil];
            }
            completer(result, error);
        }
    }];
}


#pragma mark 修改地址
+ (void)updateAddress:(NSDictionary *)param completer:(void(^)(id resultObject, NSError *error))completer {
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:UPDATEADDRESS param:param completer:^(id result, NSError *error) {
        NSDictionary *resultDic = result;
        Address *address = [Address yy_modelWithJSON:resultDic];
        if(completer) {
            completer(address, error);
        }
    }];
    
}


#pragma mark 删除地址
+ (void)deleteAddress:(NSString *)addressNo completer:(void(^)(id resultObject, NSError *error))completer {
    NSDictionary *param = @{@"addrNo" : addressNo};
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:DELETEADDRESS param:param completer:^(id result, NSError *error) {
        if(completer) {
            completer(result, error);
        }
    }];
    
}

#pragma mark 添加地址
+ (void)addNewAddress:(NSDictionary *)param completer:(void(^)(id resultObject, NSError *error))completer {
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:ADDNEWADDRESS param:param completer:^(id result, NSError *error) {
        NSDictionary *resultDic = result;
         Address *address = [Address yy_modelWithJSON:resultDic];
        if(completer) {
            completer(address, error);
        }
    }];
}

#pragma mark 查询地址
+ (void)selectAddress:(void (^)(id resultObject, NSError *error)) completer {
    BasicNetManager *manager = [BasicNetManager share];
    NSString *tel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
    NSDictionary *dic = @{@"userTel": tel};
    [manager postURL:SELECTADDRESS param:dic completer:^(id result, NSError *error) {
        NSArray *data = result;
        NSMutableArray *addresses = [[NSMutableArray alloc] init];
        for (int i = 0; i < data.count; i++) {
            Address *address = [Address yy_modelWithJSON:data[i]];
            [addresses addObject:address];
        }
        if(completer) {
            completer(addresses, error);
        }
    }];
}
@end
